home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / cli / MCommands_1_2.lha / Src / rxcontrol.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  10KB  |  408 lines

  1. /****************************************************************************/
  2. /*                               RXControl.c                                */
  3. /*                        Control features of ARexx                         */
  4. /*                    Copyright © 1994 MIchael Letowski                     */
  5. /****************************************************************************/
  6.  
  7. #define __USE_SYSBASE
  8.  
  9. #include <exec/types.h>
  10. #include <exec/execbase.h>
  11. #include <dos/rdargs.h>
  12. #include <dos/dos.h>
  13. #include <rexx/rxslib.h>
  14. #include <intuition/intuition.h>
  15. #include <workbench/startup.h>
  16.  
  17. #include <support/types.h>
  18. #include <support/exec.h>
  19. #include <support/dos.h>
  20.  
  21. #include <string.h>
  22.  
  23. #include <proto/exec.h>
  24. #include <proto/dos.h>
  25. #include <proto/rexxsyslib.h>
  26. #include <proto/intuition.h>
  27. #include <proto/icon.h>
  28.  
  29. #include "rxcontrol.rev.h"
  30.  
  31. #define DOS_NAME                "dos.library"
  32. #define REXX_NAME                "rexxsyslib.library"
  33. #define INTUI_NAME            "intuition.library"
  34. #define ICON_NAME                "icon.library"
  35. #define DOS_VERN                37L
  36. #define REXX_VERN                36L
  37. #define INTUI_VERN            37L
  38. #define ICON_VERN                37L
  39.  
  40. #define RXPORT_NAME            "REXX"
  41.  
  42. #define RXCONTROL_NAME    "RXControl"
  43.  
  44. #define TEMPLATE                "CANCEL=RXC/S,HALT=HI/S,SUSPEND/S,RESUME/S,"\
  45.                                                 "TRACESTART=TS/S,TRACEEND=TE/S,"\
  46.                                                 "CONOPEN=TCO/S,CONCLOSE=TCC/S,"\
  47.                                                 "QUIET/S"
  48.  
  49. #define OPT_RXC                    0
  50. #define OPT_HI                    1
  51. #define OPT_SUSPEND            2
  52. #define OPT_RESUME            3
  53. #define OPT_TS                    4
  54. #define OPT_TE                    5
  55. #define OPT_TCO                    6
  56. #define OPT_TCC                    7
  57. #define OPT_QUIET                8
  58. #define OPT_COUNT                9
  59.  
  60. /* Messages */
  61. #define UNABLE_OPEN            "Unable to open '%s'\n"
  62. #define NOT_ACTIVE            "ARexx server not active\n"
  63. #define IS_SUSPENDED        "Execution is suspended\n"
  64. #define WILL_CLOSE            "RexxMaster will close\n"
  65. /* Other texts */
  66. #define OK                            "OK"
  67. #define ON                            "ON"
  68. #define OFF                            "OFF"
  69. #define OPEN                        "open"
  70. #define CLOSED                    "closed"
  71. #define REQUEST_MSG            " Request"
  72. #define INFO_MSG                " Information"
  73. #define SPC_TAB                    "  "
  74.  
  75. STATIC CONST TEXT VersionString[]=
  76.     VERSION(PROG_NAME,PROG_VERSION,PROG_REVISION,PROG_DATE);
  77.  
  78. struct Switch
  79. {
  80.     STRPTR sw_Name;
  81.     ULONG sw_Index;
  82. };
  83.  
  84. ULONG CLIMode(struct ExecBase *SysBase, struct DosLibrary *DOSBase);
  85. ULONG WBMode(struct ExecBase *SysBase, struct DosLibrary *DOSBase);
  86.  
  87. ULONG RXControl(VOID)
  88. {
  89.     struct ExecBase *SysBase=*((struct ExecBase **)4);
  90.     struct DosLibrary *DOSBase;
  91.  
  92.     ULONG RC=RETURN_FAIL;
  93.  
  94.     unless(DOSBase=(struct DosLibrary *)OpenLibrary(DOS_NAME,DOS_VERN))
  95.     {
  96.         SetResult2(ERROR_INVALID_RESIDENT_LIBRARY);
  97.         goto InvalidDOS;
  98.     }
  99.  
  100.     RC=FromWB ? WBMode(SysBase,DOSBase) : CLIMode(SysBase,DOSBase);
  101.  
  102.     CloseLibrary((struct Library *)DOSBase);
  103. InvalidDOS:
  104.     return(RC);
  105. }
  106.  
  107. /* CLI mode work */
  108. ULONG CLIMode(struct ExecBase *SysBase, struct DosLibrary *DOSBase)
  109. {
  110.     struct RxsLib *RexxSysBase;
  111.  
  112.     struct RDArgs *Args;
  113.     struct MsgPort *Port;
  114.     struct RexxMsg *RXMsg;
  115.     LONG Options[OPT_COUNT];
  116.     ULONG RC=RETURN_FAIL;
  117.     BOOL ErrOccured=FALSE;
  118.  
  119.     unless(RexxSysBase=(struct RxsLib *)OpenLibrary(REXX_NAME,REXX_VERN))
  120.     {
  121.         CauseIoErr(ERROR_INVALID_RESIDENT_LIBRARY,NULL);
  122.         goto InvalidREXX;
  123.     }
  124.  
  125.     /* Read options */
  126.     clear(&Options);                                        /* Clear options buffer */
  127.     unless(Args=ReadArgs(TEMPLATE,Options,NULL))
  128.     {
  129.         PrintFault(IoErr(),NULL);                /* Inform user */
  130.         goto NoArgs;
  131.     }
  132.  
  133.     /* Check for REXX port */
  134.     Forbid();
  135.     unless(Port=FindPort(RXPORT_NAME))
  136.     {
  137.         Permit();
  138.         PutStr(NOT_ACTIVE);
  139.         goto NotActive;
  140.     }
  141.     /* RXC */
  142.     if(Options[OPT_RXC])
  143.     {
  144.         if(RXMsg=CreateRexxMsg(NULL,NULL,NULL))
  145.         {
  146.             RXMsg->rm_Action=RXCLOSE | RXFF_NONRET;
  147.             PutMsg(Port,(struct Message *)RXMsg);
  148.         }
  149.         else
  150.         {
  151.             CauseIoErr(ERROR_NO_FREE_STORE,NULL);
  152.             ErrOccured=TRUE;
  153.         }
  154.     }
  155.     /* HI */
  156.     if(Options[OPT_HI])
  157.     {
  158.         bset(RexxSysBase->rl_Flags,RLFB_HALT);
  159.         Signal(Port->mp_SigTask,flag(Port->mp_SigBit));
  160.     }
  161.     /* Suspend/Resume */
  162.     if(Options[OPT_SUSPEND])
  163.     {
  164.         bset(RexxSysBase->rl_Flags,RLFB_SUSP);
  165.         Signal(Port->mp_SigTask,flag(Port->mp_SigBit));
  166.     }
  167.     if(Options[OPT_RESUME])
  168.     {
  169.         bclr(RexxSysBase->rl_Flags,RLFB_SUSP);
  170.         Signal(Port->mp_SigTask,flag(Port->mp_SigBit));
  171.     }
  172.     /* TE/TS */
  173.     if(Options[OPT_TE])
  174.         bclr(RexxSysBase->rl_Flags,RLFB_TRACE);
  175.     if(Options[OPT_TS])
  176.         bset(RexxSysBase->rl_Flags,RLFB_TRACE);
  177.     /* TCO/TCC */
  178.     if(Options[OPT_TCO])
  179.     {
  180.         if(RXMsg=CreateRexxMsg(NULL,NULL,NULL))
  181.         {
  182.             RXMsg->rm_Action=RXTCOPN | RXFF_NONRET;
  183.             PutMsg(Port,(struct Message *)RXMsg);
  184.         }
  185.         else
  186.         {
  187.             CauseIoErr(ERROR_NO_FREE_STORE,NULL);
  188.             ErrOccured=TRUE;
  189.         }
  190.     }
  191.     if(Options[OPT_TCC])
  192.     {
  193.         if(RXMsg=CreateRexxMsg(NULL,NULL,NULL))
  194.         {
  195.             RXMsg->rm_Action=RXTCCLS | RXFF_NONRET;
  196.             PutMsg(Port,(struct Message *)RXMsg);
  197.         }
  198.         else
  199.         {
  200.             CauseIoErr(ERROR_NO_FREE_STORE,NULL);
  201.             ErrOccured=TRUE;
  202.         }
  203.     }
  204.     Permit();
  205.  
  206.     unless(Options[OPT_QUIET])
  207.     {
  208.         Printf("ARexx state:\n"
  209.                         "\tTracing is %s\n"
  210.                         "\tTrace console is %s\n%s%s",
  211.                         btst(RexxSysBase->rl_Flags,RLFB_TRACE) ? ON : OFF,
  212.                         RexxSysBase->rl_TraceFH ? OPEN : CLOSED,
  213.                         btst(RexxSysBase->rl_Flags,RLFB_SUSP) ?
  214.                             "\t" IS_SUSPENDED : NULL,
  215.                         btst(RexxSysBase->rl_Flags,RLFB_CLOSE) ?
  216.                             "\t" WILL_CLOSE : NULL);
  217.     }
  218.  
  219.     RC=(ErrOccured ? RETURN_ERROR : RETURN_OK);
  220.  
  221. NotActive:
  222.     FreeArgs(Args);
  223. NoArgs:
  224.     CloseLibrary((struct Library *)RexxSysBase);
  225. InvalidREXX:
  226.     return(RC);
  227. }
  228.  
  229. /* Workbench mode work */
  230. ULONG WBMode(struct ExecBase *SysBase, struct DosLibrary *DOSBase)
  231. {
  232.     struct IntuitionBase *IntuitionBase;
  233.     struct RxsLib *RexxSysBase;
  234.     struct Library *IconBase;
  235.  
  236.     STATIC CONST struct EasyStruct NoLibReq=
  237.     {
  238.         sizeof(struct EasyStruct),0,
  239.         RXCONTROL_NAME REQUEST_MSG,
  240.         UNABLE_OPEN,
  241.         OK
  242.     };
  243.     STATIC CONST struct EasyStruct NotActiveReq=
  244.     {
  245.         sizeof(struct EasyStruct),0,
  246.         RXCONTROL_NAME REQUEST_MSG,
  247.         NOT_ACTIVE,
  248.         OK
  249.     };
  250.     STATIC CONST struct EasyStruct InfoReq=
  251.     {
  252.         sizeof(struct EasyStruct),0,
  253.         RXCONTROL_NAME INFO_MSG,
  254.         "ARexx state:\n"
  255.             SPC_TAB "Tracing is %s\n"
  256.             SPC_TAB "Trace console is %s\n%s%s",
  257.         OK
  258.     };
  259.     STATIC CONST struct Switch Switches[]=
  260.     {
  261.         {"CANCEL",OPT_RXC},            {"RXC",OPT_RXC},
  262.         {"HALT",OPT_HI},                {"HI",OPT_HI},
  263.         {"SUSPEND",OPT_SUSPEND},
  264.         {"RESUME",OPT_RESUME},
  265.         {"TRACESTART",OPT_TS},    {"TS",OPT_TS},
  266.         {"TRACEEND",OPT_TE},        {"TE",OPT_TE},
  267.         {"CONOPEN",OPT_TCO},        {"TCO",OPT_TCO},
  268.         {"CONCLOSE",OPT_TCC},        {"TCC",OPT_TCC},
  269.         {"QUIET",OPT_QUIET}
  270.     };
  271.  
  272.     struct WBArg *Args;
  273.     struct MsgPort *Port;
  274.     struct RexxMsg *RXMsg;
  275.     struct WBStartup *WBMsg;
  276.     struct DiskObject *Object;
  277.     STRPTR *ToolArray;
  278.     BPTR OldDir;
  279.     LONG Options[OPT_COUNT];
  280.     ULONG I,J,RC=RETURN_FAIL;
  281.     BOOL ErrOccured=FALSE;
  282.  
  283.     WaitPort(&ThisProcessS->pr_MsgPort);
  284.     WBMsg=(struct WBStartup *)GetMsg(&ThisProcessS->pr_MsgPort);
  285.     unless(IntuitionBase=
  286.                     (struct IntuitionBase *)OpenLibrary(INTUI_NAME,INTUI_VERN))
  287.     {
  288.         SetResult2(ERROR_INVALID_RESIDENT_LIBRARY);
  289.         goto InvalidIntuition;
  290.     }
  291.     unless(IconBase=OpenLibrary(ICON_NAME,ICON_VERN))
  292.     {
  293.         EasyRequest(NULL,&NoLibReq,NULL,ICON_NAME);
  294.         goto InvalidIcon;
  295.     }
  296.     unless(RexxSysBase=(struct RxsLib *)OpenLibrary(REXX_NAME,REXX_VERN))
  297.     {
  298.         EasyRequest(NULL,&NoLibReq,NULL,REXX_NAME);
  299.         goto InvalidREXX;
  300.     }
  301.  
  302.     clear(&Options);                                        /* Clear options buffer */
  303.     for(I=0, Args=WBMsg->sm_ArgList; I<WBMsg->sm_NumArgs; I++, Args++)
  304.     {
  305.         if(Args->wa_Lock && *Args->wa_Name)
  306.         {
  307.             OldDir=CurrentDir(Args->wa_Lock);
  308.             if(*Args->wa_Name && (Object=GetDiskObject(Args->wa_Name)))
  309.             {
  310.                 ToolArray=(STRPTR *)Object->do_ToolTypes;
  311.                 for(J=0; J<sizeof(Switches)/sizeof(struct Switch); J++)
  312.                     if(FindToolType(ToolArray,Switches[J].sw_Name))
  313.                         Options[Switches[J].sw_Index]=TRUE;
  314.                 FreeDiskObject(Object);
  315.             }
  316.             CurrentDir(OldDir);                            /* CD back */
  317.         }
  318.     }
  319.  
  320.     /* Check for REXX port */
  321.     Forbid();
  322.     unless(Port=FindPort(RXPORT_NAME))
  323.     {
  324.         Permit();
  325.         EasyRequest(NULL,&NotActiveReq,NULL);
  326.         goto NotActive;
  327.     }
  328.     /* RXC */
  329.     if(Options[OPT_RXC])
  330.     {
  331.         if(RXMsg=CreateRexxMsg(NULL,NULL,NULL))
  332.         {
  333.             RXMsg->rm_Action=RXCLOSE | RXFF_NONRET;
  334.             PutMsg(Port,(struct Message *)RXMsg);
  335.         }
  336.         else
  337.             ErrOccured=TRUE;
  338.     }
  339.     /* HI */
  340.     if(Options[OPT_HI])
  341.     {
  342.         bset(RexxSysBase->rl_Flags,RLFB_HALT);
  343.         Signal(Port->mp_SigTask,flag(Port->mp_SigBit));
  344.     }
  345.     /* Suspend/Resume */
  346.     if(Options[OPT_SUSPEND])
  347.     {
  348.         bset(RexxSysBase->rl_Flags,RLFB_SUSP);
  349.         Signal(Port->mp_SigTask,flag(Port->mp_SigBit));
  350.     }
  351.     if(Options[OPT_RESUME])
  352.     {
  353.         bclr(RexxSysBase->rl_Flags,RLFB_SUSP);
  354.         Signal(Port->mp_SigTask,flag(Port->mp_SigBit));
  355.     }
  356.     /* TE/TS */
  357.     if(Options[OPT_TE])
  358.         bclr(RexxSysBase->rl_Flags,RLFB_TRACE);
  359.     if(Options[OPT_TS])
  360.         bset(RexxSysBase->rl_Flags,RLFB_TRACE);
  361.     /* TCO/TCC */
  362.     if(Options[OPT_TCO])
  363.     {
  364.         if(RXMsg=CreateRexxMsg(NULL,NULL,NULL))
  365.         {
  366.             RXMsg->rm_Action=RXTCOPN | RXFF_NONRET;
  367.             PutMsg(Port,(struct Message *)RXMsg);
  368.         }
  369.         else
  370.             ErrOccured=TRUE;
  371.     }
  372.     if(Options[OPT_TCC])
  373.     {
  374.         if(RXMsg=CreateRexxMsg(NULL,NULL,NULL))
  375.         {
  376.             RXMsg->rm_Action=RXTCCLS | RXFF_NONRET;
  377.             PutMsg(Port,(struct Message *)RXMsg);
  378.         }
  379.         else
  380.             ErrOccured=TRUE;
  381.     }
  382.     Permit();
  383.  
  384.     unless(Options[OPT_QUIET])
  385.     {
  386.         EasyRequest(NULL,&InfoReq,NULL,
  387.                                 btst(RexxSysBase->rl_Flags,RLFB_TRACE) ? ON : OFF,
  388.                                 RexxSysBase->rl_TraceFH ? OPEN : CLOSED,
  389.                                 btst(RexxSysBase->rl_Flags,RLFB_SUSP) ?
  390.                                     SPC_TAB IS_SUSPENDED : NULL,
  391.                                 btst(RexxSysBase->rl_Flags,RLFB_CLOSE) ?
  392.                                     SPC_TAB WILL_CLOSE : NULL);
  393.     }
  394.  
  395.     RC=(ErrOccured ? RETURN_ERROR : RETURN_OK);
  396.  
  397. NotActive:
  398. NoArgs:
  399.     CloseLibrary((struct Library *)RexxSysBase);
  400. InvalidREXX:
  401.     CloseLibrary(IconBase);
  402. InvalidIcon:
  403.     CloseLibrary((struct Library *)IntuitionBase);
  404. InvalidIntuition:
  405.     ReplyMsg((struct Message *)WBMsg);
  406.     return(RC);
  407. }
  408.